<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增资产领用')" />
    <th:block th:include="include :: datetimepicker-css" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-receive-add">
            <h4 class="form-header h4">资产领用信息</h4>
            <div class="form-group">
                <label class="col-sm-3 control-label">类型：</label>
                <div class="col-sm-8">
                    <select name="receiveType" class="form-control m-b" th:with="type=${@dict.getType('receive_type')}">
                        <option value="2">借用</option>
                    </select>
                </div>
            </div>
            <div class="form-group" style="display: none;">
                <label class="col-sm-3 control-label">借用人编号：</label>
                <div class="col-sm-8">
                    <input name="userId" class="form-control" type="text" th:value="${userid}" required >
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required" >借用人：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="userName" class="form-control" type="text" readonly="readonly" th:value="${username}" required>
                        <span class="input-group-addon input-sm" onclick="selectUser()">
                            <i class="fa fa-search"></i>
                        </span>
                    </div>
                </div>

            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label">借用日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="receiveTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">预计归还日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="predictTime" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label">借用编号：</label>
                <div class="col-sm-8">
                    <input name="receiveNo" class="form-control" type="text">
                </div>
            </div>
            <!--<div class="form-group">    -->
                <!--<label class="col-sm-3 control-label">0：正常 1：删除：</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<input name="deleteFlag" class="form-control" type="text">-->
                <!--</div>-->
            <!--</div>-->
            <div class="form-group">    
                <label class="col-sm-3 control-label">附件：</label>
                <div class="col-sm-8">
                    <input type="hidden" name="receiveFile">
                    <div class="file-loading">
                        <input class="form-control file-upload" id="receiveFile" name="file" type="file">
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">备注：</label>
                <div class="col-sm-8">
                    <input name="receiveDesc" class="form-control" type="text">
                </div>
            </div>
            <!--<div class="form-group">-->
                <!--<label class="col-sm-3 control-label">资产编号：</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<input name="assetsId" class="form-control" type="text">-->
                <!--</div>-->
            <!--</div>-->

            <!--<div class="form-group">-->
                <!--<label class="col-sm-3 control-label">SN号：</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<input name="assetsSn" class="form-control" type="text">-->
                <!--</div>-->
            <!--</div>-->
            <!--<div class="form-group">-->
                <!--<label class="col-sm-3 control-label">资产名称：</label>-->
                <!--<div class="col-sm-8">-->
                    <!--<input name="assetsName" class="form-control" type="text">-->
                <!--</div>-->
            <!--</div>-->
            <!-- -->
            <h4 class="form-header h4">借用资产信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-white btn-sm" onclick="selectAssets()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-white btn-sm" onclick="sub.delRow()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
                        <table id="bootstrap-table"></table>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">

        $(document).ready(function(){
            var currtime = new Date().Format("yyyy-MM-dd");
            $("*[name='receiveTime']").val(currtime);

            var receiveno = new Date().Format("yyyyMMddHHmmss");
            $("*[name='receiveNo']").val('BO'+receiveno);

        });

        /* 选择用户 */
        function selectUser() {
            var url = prefix + '/selectUser';
            var options = {
                title: '选择借用人',
                width: "800",
                url: url,
                callBack: doSelectUserSubmit
            };
            $.modal.openOptions(options);
        }

        function doSelectUserSubmit(index, layero){
            var body = $.modal.getChildFrame(index);
            var suserid = body.find('#suserid').val();
            var susername = body.find('#susername').val();
            $.modal.close(index);
            $("*[name='userId']").val(suserid);
            $("*[name='userName']").val(susername);
        }

        var prefix = ctx + "assets/borrow"
        var assetsTypeDatas = [[${@dict.getType('assets_type')}]];
        var assetsAreaDatas = [[${@dict.getType('area_type')}]];
        var assetsCompanyDatas = [[${@dict.getType('company')}]];
        var assetsStatusDatas = [[${@dict.getType('assets_status')}]];
        $("#form-receive-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-receive-add').serialize());
            }
        }

        $("input[name='receiveTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='predictTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $(".file-upload").fileinput({
            uploadUrl: ctx + 'common/upload',
            maxFileCount: 1,
            autoReplace: true
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })

        $(function() {
            var options = {
                pagination: false,
                showToggle: false,
                showColumns: false,
                sidePagination: "client",
                showSearch: false,
                showRefresh: false,
                uniqueId: "id",
                columns: [{
                    checkbox: true
                },
                {
                    field: 'index',
                    align: 'center',
                    title: "序号",
                    formatter: function (value, row, index) {
                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
                    	return columnIndex + $.table.serialNumber(index);
                    }
                }, {
                        field: 'id',
                        align: 'center',
                        title: '资产编号',
                        formatter: function(value, row, index) {
                            var html = $.common.sprintf("<input class='form-control' readonly='readonly' type='text' name='assetsInfoList[%s].id' value='%s'>", index, value);
                            return html;
                        }
                    },
                {
                    field: 'assetsName',
                    align: 'left',
                    title: '资产名称',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' readonly='readonly' type='text' name='assetsInfoList[%s].assetsName' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'assetsType',
                    align: 'left',
                    title: '资产类别',
                    formatter: function(value, row, index) {
                        var name = $.common.sprintf("assetsInfoList[%s].assetsType", index);
                        return $.common.dictToSelectReadonly(assetsTypeDatas, value, name);
//                        return $.table.selectDictLabel(assetsTypeDatas, value);
                    }
                },
                {
                    field: 'assetsModel',
                    align: 'left',
                    title: '规格型号',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' readonly='readonly' type='text' name='assetsInfoList[%s].assetsModel' value='%s'>", index, value);
                        return html;
                    }
                },
                {
                    field: 'assetsSn',
                    align: 'left',
                    title: 'SN号',
                    formatter: function(value, row, index) {
                        var html = $.common.sprintf("<input class='form-control' readonly='readonly' type='text' name='assetsInfoList[%s].assetsSn' value='%s'>", index, value);
                        return html;
                    }
                }]
            };
            $.table.init(options);
        });


        function selectAssets() {
            var url = prefix + '/selectAssets/0';
            var options = {
                title: '选择设备',
                width: "800",
                url: url,
                callBack: doSelectAssetsSubmit
            };
            $.modal.openOptions(options);
        }


        function doSelectAssetsSubmit(index, layero) {
            var body = $.modal.getChildFrame(index);

            var aid = body.find('#aid').val();
            var aassetsName = body.find('#aassetsName').val();
            var aassetsType = body.find('#aassetsType').val();
            var aassetsModel = body.find('#aassetsModel').val();
            var aassetsSn = body.find('#aassetsSn').val();

            $.modal.close(index);

            if(aid==''){
                return;
            }

            //过滤已经选择的资产数据
            var data = $("#" + table.options.id).bootstrapTable('getRowByUniqueId', aid);
            if(data != null){
                return;
            }

            var count = $("#" + table.options.id).bootstrapTable('getData').length;
            var row = {
                index: $.table.serialNumber(count),
                id: aid ,
                assetsName: aassetsName,
                assetsType: aassetsType,
                assetsModel: aassetsModel,
                assetsSn: aassetsSn
            }
            sub.addRow(row);
        }



        Date.prototype.Format = function (fmt) {
            var o = {
                "M+": this.getMonth() + 1, //月份
                "d+": this.getDate(), //日
                "H+": this.getHours(), //小时
                "m+": this.getMinutes(), //分
                "s+": this.getSeconds(), //秒
                "q+": Math.floor((this.getMonth() + 3) / 3), //季度
                "S": this.getMilliseconds() //毫秒
            };
            if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
            return fmt;
        }

    </script>
</body>
</html>